/*
 * Filename CheckDataFactory.java 2021年09月26日
 * Copyright © Ehome Co.Ltd. All Rgiths Reserved.
 * @author wuyingchao
 */
package com.ehome.poct.core.factory;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.ehome.poct.base.api.dto.device.TestDeviceDTO;
import com.ehome.poct.core.api.dto.check.*;
import com.ehome.poct.base.api.dto.doctor.ClinicDTO;
import com.ehome.poct.core.api.vo.check.CheckProcessResultVO;
import com.ehome.poct.core.api.vo.check.CheckStatusVO;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * 检测数据 工厂
 *
 * @author cq
 * @since 2022/03/08 11:52
 **/
public class CheckDataFactory {
    public static CellCheckDataDTO toCheckData(CheckParamDTO cmd, AlgoRequestLog requestLog) {
        return new CellCheckDataDTO(requestLog.getId(), cmd.getClinicId(), cmd.getPatientId(), cmd.getType() == 1 ? 100 : 121,
                cmd.getDeviceSno(), cmd.getBoardSno(), 1, cmd.getPath(),null);
    }

    public static CellAlgoRequestLog createAlgoRequestLog(String reqUrl, CellAlgoAnalysisDTO request, String responseContent, String sno, String filePath) {
        return new CellAlgoRequestLog(Long.valueOf(request.getId()), reqUrl, responseContent, sno, filePath,request);
    }

    public static CellAlgoAnalysisDTO toAlgoAnalysisDTO(CheckParamDTO cmd, SampleMaterialDTO sample, String callBackUrl) {
        CellCheckExtendParamDTO param = JSONUtil.toBean(cmd.getExtendInfo(),CellCheckExtendParamDTO.class);
        CellBoardDTO board = (CellBoardDTO) sample;
        var batchInfo = BeanUtil.toBean(board,BatchInfoDTO.class);
        return new CellAlgoAnalysisDTO().setId(IdUtil.getSnowflake().nextIdStr())
                .setThickness(toBoardMap(board))
                .setConsumableId(board.getBoardSno())
                .setDeviceId(cmd.getDeviceSno())
                .setHgbMeasure(param.getHgbMeasure())
                .setHgbReference(param.getHgbReference())
                .setHgbCurveData(param.getHgbCurveData())
                .setHgbCalibration(toList(param.getHgbCalibration()))
                .setPath(cmd.getPath())
                .setCallBackUrl(callBackUrl)
                .setPointConfig(toPointConfigDTO(param.getPointConfig()))
                .setBoardThickness(board.getBoardThickness())
                .setCameraName(param.getCameraName())
                .setBatchInfo(batchInfo);
    }

    public static List<HgbCalibrationDTO> toList(List<HgbCalibration> list) {
        return list.stream().map(CheckDataFactory::toHgbCalibrationDTO).collect(Collectors.toList());
    }

    public static Map<String, BigDecimal> toBoardMap(CellBoardDTO board) {
        final var map = new HashMap<String, BigDecimal>(5);
        map.put("0", board.getBoardThicknessA());
        map.put("1", board.getBoardThicknessB());
        map.put("2", board.getBoardThicknessC());
        return map;
    }

    public static HgbCalibrationDTO toHgbCalibrationDTO(HgbCalibration vo) {
        return new HgbCalibrationDTO().setMeasure(vo.getMeasure()).setReference(vo.getReference()).setGroundTruth(vo.getGroundTruth());
    }

    public static PointConfigDTO toPointConfigDTO(PointConfig vo) {
        return Optional.ofNullable(vo)
                .map(i -> new PointConfigDTO().setRbc(toPointDtlDTO(i.getRbc())).setPlt(toPointDtlDTO(i.getPlt())).setWbc(toPointDtlDTO(i.getWbc()))).orElse(null);
    }

    public static PointConfigDTO.PointDtlDTO toPointDtlDTO(PointConfig.PointDtlVO vo) {
        return new PointConfigDTO.PointDtlDTO(vo.getPoints(), vo.getFocus());
    }
    public static CheckProcessResultVO toCellCheckResultVO(TestDeviceDTO device,
                                                           ClinicDTO clinic, CellBoardDTO board,
                                                           AlgoRequestLog algoLog, CellCheckDataDTO checkData){
        return new CheckProcessResultVO(checkData.getId(),100L,"","血常规检测",null);
    }

    public static CheckProcessResultVO toGoldCheckResultVO(TestDeviceDTO device,
                                                           ClinicDTO clinic, CellBoardDTO board,
                                                           AlgoRequestLog algoLog, GoldCheckDataDTO checkData){
        return new CheckProcessResultVO(checkData.getId(),100L,"","血常规检测",null);
    }
    public static CheckStatusVO toCheckStatusVO(CheckStatus checkStatus) {
        return new CheckStatusVO(checkStatus.getStatus(), checkStatus.getMsg());
    }
    public static CheckStatusVO toCheckStatusVO(CheckDataDTO data) {
        return new CheckStatusVO(data.getStatus(), data.getAbnormalMsg());
    }
}